const express = require('express')
const app = express()
const superagent = require('superagent')
const cherrio = require('cheerio')
const Nightmare = require('nightmare')
const nightmare = Nightmare({
    show: true
})

app.listen(3000, () => {
    console.log('server is starting...')
})

let getHotNews = res => {
    let hotNews = []
    let $ = cherrio.load(res.text)
    $('div#pane-news .hotnews ul li a').each((index, element) => {
        let news = {
            title: $(element).text(),
            href: $(element).attr('href')
        }
        hotNews.push(news)
    })
    return hotNews
}

superagent.get('http://news.baidu.com/').end((err, res) => {
    if (err) {
        console.log('catch error')
    } else {
        hotNews = getHotNews(res)
    }
})

// 抓取需要加载的数据
nightmare
    .goto('http://news.baidu.com/')
    .wait('div#local_news')
    .evaluate(() => document.querySelector('div#local_news').innerHTML)
    .then(htmlStr => {
        localNews = getLocalNews(htmlStr)
    })
    .catch(err => {
        console.log('localNews catch error', err)
    })

app.get('/', async (req, res, next) => {
    res.send({
        hotNews,
        localNews
    })
})